#include "discovery.h"
#include "connectivitymanager.h"
#include "generatetools.h"
#include "connect_plugin.h"

#include <thread>
#include <iostream>
#include <QObject>
#include <QString>

// #define  extern "C" __attribute__((visibility("default"))) __attribute__((used))

std::thread discoveryThread;
bool flag = false;

KDiscovery::Discovery *discovery = new KDiscovery::Discovery();

void StartDiscovery()
{
    if (flag) {
        // 打印错误，防止重复启动
        std::cout << "already start discovery\n";
        return;
    }
    flag = true;
    discoveryThread = std::thread([]() {
        discovery->startDiscovery();
    });
}

void StopDiscovery() 
{
    flag = false;
    discovery->stopDiscovery();
    if (discoveryThread.joinable()) {
        discoveryThread.join();
    }
}

void StartSessionServer()
{
    discovery->startSessionServer();
}

void StopSessionServer()
{
    discovery->stopSessionServer();
}

void TranFileTransfer()
{
    discovery->sendSessionRequest(0);
}

void TranScreenProjection()
{
    discovery->sendSessionRequest(1);
}

void AddDeviceListener(void (*callback)(std::string&& deviceInfo))
{
    discovery->addDeviceListener(callback);
}

const char * GetAllDeviceInfo() {
    return discovery->GetAllDeviceInfo();
}